package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.goods.business.converter.ProductCategoryConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategoryTreeNode;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: 赵勇镔
 * @Time: 2021/6/7 16:03
 * @Description:
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired(required = false)
    private ProductCategoryMapper productCategoryMapper;

    @Autowired(required = false)
    private ProductMapper productMapper;

    //static  int n = 0;

    //查询一二三级分类
    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        //调用递归,获取返回结果
        //List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs = Children(0L, materialCategoryMapper);

        //查询所有一级分类
        //创建查询条件 pid=0
        Example o = new Example(ProductCategoryTreeNode.class);
        o.createCriteria().andEqualTo("pid",0);
        //获取一级分类列表
        List<ProductCategoryTreeNode> productCategoryTreeNodes = productCategoryMapper.selectByExample(o);

        //转换对象
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs = new ArrayList<>();
        //判断查询列表是否为空
        if (!CollectionUtils.isEmpty(productCategoryTreeNodes)) {
            //循环遍历
            for (ProductCategoryTreeNode productCategoryTreeNode : productCategoryTreeNodes) {


                //通过一级分类查询二三级分类 通过一级id查询匹配的pid并添加到父类集合属性中

                Example o1 = new Example(ProductCategoryTreeNode.class);
                o1.createCriteria().andEqualTo("pid",productCategoryTreeNode.getId());
                List<ProductCategoryTreeNode> productCategoryTreeNodes1 = productCategoryMapper.selectByExample(o1);

                List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs1 = new ArrayList<>();
                //非空判断
                if(!CollectionUtils.isEmpty(productCategoryTreeNodes1)){
                    for (ProductCategoryTreeNode productCategoryTreeNode1 : productCategoryTreeNodes1){

                        //查询三级分类
                        Example o2 = new Example(ProductCategoryTreeNode.class);
                        o2.createCriteria().andEqualTo("pid",productCategoryTreeNode1.getId());
                        List<ProductCategoryTreeNode> productCategoryTreeNodes2 = productCategoryMapper.selectByExample(o2);
                        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs2 = new ArrayList<>();
                        if(!CollectionUtils.isEmpty(productCategoryTreeNodes2)){
                            for(ProductCategoryTreeNode productCategoryTreeNode2 : productCategoryTreeNodes2){

                                ProductCategoryTreeNodeVO d = new ProductCategoryTreeNodeVO();
                                //工具类赋值
                                BeanUtils.copyProperties(productCategoryTreeNode2, d);
                                //设置分类等级
                                d.setLev(3);
                                productCategoryTreeNodeVOs2.add(d);
                            }
                        }

                        //子类赋值
                        productCategoryTreeNode1.setChildren(productCategoryTreeNodeVOs2);

                        ProductCategoryTreeNodeVO d = new ProductCategoryTreeNodeVO();
                        //工具类赋值
                        BeanUtils.copyProperties(productCategoryTreeNode1, d);
                        //设置分类等级
                        d.setLev(2);
                        productCategoryTreeNodeVOs1.add(d);
                    }
                }
                //子类赋值
                productCategoryTreeNode.setChildren(productCategoryTreeNodeVOs1);
                ProductCategoryTreeNodeVO d = new ProductCategoryTreeNodeVO();
                //工具类赋值
                BeanUtils.copyProperties(productCategoryTreeNode, d);
                //设置分类等级
                d.setLev(1);
                productCategoryTreeNodeVOs.add(d);
            }
        }


        //转化为分页对象
        PageInfo<ProductCategoryTreeNode> Info = new PageInfo<>(productCategoryTreeNodes);
        //前端返回需要的对象
        return new PageVO<>(Info.getTotal(), productCategoryTreeNodeVOs);
    }

    //递归方法
    public static List<ProductCategoryTreeNodeVO> Children(Long pid, ProductCategoryMapper materialCategoryMapper){
        //创建查询条件
        Example o = new Example(ProductCategoryTreeNode.class);
        //根据pid查询
        o.createCriteria().andEqualTo("pid",pid);
        //获取当前级分类列表
        List<ProductCategoryTreeNode> productCategoryTreeNodes = materialCategoryMapper.selectByExample(o);
        //转换对象
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs = new ArrayList<>();
        //判断是否为空 为空则跳出递归
        if(!CollectionUtils.isEmpty(productCategoryTreeNodes)){
            //n++;
            //循环遍历
            for (ProductCategoryTreeNode productCategoryTreeNode : productCategoryTreeNodes) {
                //递归调用
                List<ProductCategoryTreeNodeVO> children = Children(productCategoryTreeNode.getId(), materialCategoryMapper);
                //vo转化类对象 转化
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = ProductCategoryConverter
                        .converterProductCategoryTreeNodeVO(productCategoryTreeNode);
                //给子菜单赋值
                productCategoryTreeNodeVO.setChildren(children);
                //给子菜单赋值
                productCategoryTreeNodeVO.setLev(1);
                //添加到集合
                productCategoryTreeNodeVOs.add(productCategoryTreeNodeVO);
            }
        }
        //返回对象
        return productCategoryTreeNodeVOs;
    }

    //查询所有父类
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTre() {
        //查询所有一级分类
        //创建查询条件 pid=0
        Example o = new Example(ProductCategoryTreeNode.class);
        o.createCriteria().andEqualTo("pid",0);
        //获取一级分类列表
        List<ProductCategoryTreeNode> productCategoryTreeNodes = productCategoryMapper.selectByExample(o);

        //转换对象
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs = new ArrayList<>();
        //判断查询列表是否为空
        if (!CollectionUtils.isEmpty(productCategoryTreeNodes)) {
            //循环遍历
            for (ProductCategoryTreeNode productCategoryTreeNode : productCategoryTreeNodes) {

                Example o1 = new Example(ProductCategoryTreeNode.class);
                o1.createCriteria().andEqualTo("pid",productCategoryTreeNode.getId());
                //获取一级分类列表
                List<ProductCategoryTreeNode> productCategoryTreeNodes1 = productCategoryMapper.selectByExample(o1);
                List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
                for (ProductCategoryTreeNode productCategoryTreeNode1:productCategoryTreeNodes1) {
                    ProductCategoryTreeNodeVO productCategoryTreeNodeVO = ProductCategoryConverter.
                            converterProductCategoryTreeNodeVO(productCategoryTreeNode1);
                    productCategoryTreeNodeVOS.add(productCategoryTreeNodeVO);
                }
                productCategoryTreeNode.setChildren(productCategoryTreeNodeVOS);
                //使用工具类转化vo
                productCategoryTreeNodeVOs.add(ProductCategoryConverter.
                        converterProductCategoryTreeNodeVO(productCategoryTreeNode));
            }
        }
        //前端返回需要的对象
        return productCategoryTreeNodeVOs;
    }

    //添加分类
    @Override
    public Boolean add(ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {
        //先转化为实体类 然后存到数据库
        ProductCategoryTreeNode productCategoryTreeNode = new ProductCategoryTreeNode();

        //工具类赋值
        BeanUtils.copyProperties(productCategoryTreeNodeVO,productCategoryTreeNode);

        //设置创建和修改时间
        productCategoryTreeNode.setCreateTime(new Date());
        productCategoryTreeNode.setModifiedTime(new Date());


        int insert = productCategoryMapper.insert(productCategoryTreeNode);

        return insert>0;
    }

    //修改分类
    @Override
    public ProductCategoryTreeNodeVO edit(Long id) {

        Example o = new Example(ProductCategoryTreeNode.class);
        o.createCriteria().andEqualTo("id",id);
        ProductCategoryTreeNode productCategoryTreeNode = productCategoryMapper.selectOneByExample(o);

        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
        BeanUtils.copyProperties(productCategoryTreeNode,productCategoryTreeNodeVO);

        return productCategoryTreeNodeVO;
    }

    //修改并保存
    @Override
    public Boolean update(Long id ,ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {

        ProductCategoryTreeNode productCategoryTreeNode = new ProductCategoryTreeNode();
        //工具类赋值
        BeanUtils.copyProperties(productCategoryTreeNodeVO,productCategoryTreeNode);
        //设置修改时间
        productCategoryTreeNode.setModifiedTime(new Date());

        //修改条件
        Example o = new Example(ProductCategoryTreeNode.class);
        o.createCriteria().andEqualTo("id",id);

        //根据条件修改
        productCategoryMapper.updateByExample(productCategoryTreeNode,o);
        return null;
    }

    //根据id删除物资信息
    @Override
    public Boolean delete(Long id) {
        //先查询是否为三级分类或物资引用

        Example o = new Example(ProductCategoryTreeNode.class);
        o.createCriteria().andEqualTo("id",id);

        //先通过id查询到物资信息
        ProductCategoryTreeNode productCategoryTreeNode = productCategoryMapper.selectOneByExample(o);

        //判断物资是否有子分类
        Example o1 = new Example(ProductCategoryTreeNode.class);
        o1.createCriteria().andEqualTo("pid",id);
        List<ProductCategoryTreeNode> productCategoryTreeNodes = productCategoryMapper.selectByExample(o1);

        //判断是否有物资引用 根据id查询 引用表中 123级引用是否有相同值

        Example o2 = new Example(Product.class);
        o2.createCriteria().andEqualTo("threeCategoryId",id);
        List<Product> products = productMapper.selectByExample(o2);


        //如果子集为空并且引用为空才可以删除
        if(CollectionUtils.isEmpty(productCategoryTreeNodes)&&CollectionUtils.isEmpty(products)){

            return  productCategoryMapper.delete(productCategoryTreeNode)>0;
        }else {
            return false;
        }
    }


}
